import DB


def writeToMyDB(activityId, title, createTime, activityTime, address, imageUrl, activityUrl):
    # 用活动ID去查名称的sql语句
    selectSql = '''select title from activity where activityId = ''' + str(activityId)
    # 调用数据库方法
    results = DB.connect_mysql(selectSql, None)
    # 如果查找不到title，即数据库没有这个活动
    if len(results) == 0:
        insertSql = '''insert into activity (activityId,title,createTime,activityTime,address,imgUrl,
                                                                            activityUrl) values(%s,%s,%s,%s,%s,%s,%s)'''
        news = [activityId, title, createTime, activityTime, address, imageUrl, activityUrl]
        DB.connect_mysql(insertSql, news)
    # 如果这个活动跟数据库里面的名字不一样
    elif results[0][0] != title:
        insertSql = '''insert into activity (activityId,title,createTime,activityTime,address,imgUrl,
                                                                                    activityUrl) values(%s,%s,%s,%s,%s,%s,%s)'''
        news = [activityId, title, createTime, activityTime, address, imageUrl, activityUrl]
        DB.connect_mysql(insertSql, news)
    else:
        print(activityId, '已存在,无法插入')
